﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QuanLyRapChieuPhim.BUS;
using QuanLyRapChieuPhim.DTO;

namespace QuanLyRapChieuPhim.GUI
{
    public partial class uscThemPhim : UserControl
    {
        int Ma = -1;
        string ImagePath = "";
        string TrailerPath = "";

        public uscThemPhim()
        {
            InitializeComponent();
        }

        private void uscThemPhim_Load(object sender, EventArgs e)
        {
            Ma = int.Parse(Tag.ToString());
            LoadDefault();
        }

        private void btnCapNhap_Click(object sender, EventArgs e)
        {
            Ma = int.Parse(Tag.ToString());
            if (!Check())
                return;
            PhimBUS BUS = new PhimBUS();
            PhimDTO DTO = new PhimDTO
                (
                Ma, txtTenPhim.Text, int.Parse(cbNamSanXuat.Text), txtTenPhim.Text,
                txtGioiThieuPhim.Text, txtTrailer.Text, txtPoster.Text, int.Parse(txtThoiLuong.Text),
                int.Parse(cbDinhDang.SelectedValue.ToString()), int.Parse(cbTrangThai.SelectedValue.ToString())
                 );
            if (BUS.Update(DTO))
            {
                if (!File.Exists("../../Images/" + DTO.Poster))
                    File.Copy(ImagePath, "../../Images/" + DTO.Poster);

                MessageBox.Show("Cập nhật thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Cập nhật thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Ma = 0;
            if (!Check())
                return;
            PhimBUS BUS = new PhimBUS();
            PhimDTO DTO = new PhimDTO
                (
                Ma,txtTenPhim.Text ,int.Parse(cbNamSanXuat.Text),txtTenPhim.Text,
                txtGioiThieuPhim.Text, txtTrailer.Text, txtPoster.Text, int.Parse(txtThoiLuong.Text),
                int.Parse(cbDinhDang.SelectedValue.ToString()), int.Parse(cbTrangThai.SelectedValue.ToString())
                 );
            if (BUS.Add(DTO))
            {
                if (!File.Exists("../../Images/" + DTO.Poster))
                    File.Copy(ImagePath, "../../Images/" + DTO.Poster);
                
                MessageBox.Show("Thêm thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);
                return;
            }
            MessageBox.Show("Thêm thất bại.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.None);

        }

        private void btnDienLai_Click(object sender, EventArgs e)
        {
            LoadDefault();
        }

        private void btnBrowsTrailer_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                TrailerPath = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("\\") + 1, openFileDialog1.FileName.Length - openFileDialog1.FileName.LastIndexOf("\\") - 1);

                txtTrailer.Text = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("\\") + 1, openFileDialog1.FileName.Length - openFileDialog1.FileName.LastIndexOf("\\") - 1);
            }
        }

        private void btnBrowsPoster_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                ImagePath = openFileDialog2.FileName;
                ptbHinhAnhPhim.Image = new Bitmap(ImagePath);
                txtPoster.Text = openFileDialog2.FileName.Substring(openFileDialog2.FileName.LastIndexOf("\\") + 1, openFileDialog2.FileName.Length - openFileDialog2.FileName.LastIndexOf("\\") - 1);
            }
        }
    
        //=========================================================================================================

        void LoadDefault()
        {
            try
            {
                cbDinhDang.DataSource = new LoaiKyThuatBUS().GetList();
                cbDinhDang.DisplayMember = "TenLoaiKyThuat";
                cbDinhDang.ValueMember = "MaLoaiKyThuat";

                cbTrangThai.DataSource = new TrangThaiPhimBUS().GetList();
                cbTrangThai.DisplayMember = "TenTrangThaiPhim";
                cbTrangThai.ValueMember = "MaTrangThaiPhim";

                for (int i = 1990; i <= DateTime.Now.Year; i++)
                    cbNamSanXuat.Items.Add(i);


                PhimDTO DTO = new PhimBUS().GetFromId(Ma);
                txtGioiThieuPhim.Text = DTO.GioiThieuPhim;
                txtHangSanXuat.Text = DTO.HangSanXuat;
                txtPoster.Text = DTO.Poster;
                txtTenPhim.Text = DTO.TenPhim;
                txtThoiLuong.Text = DTO.ThoiLuongPhim.ToString();
                txtTrailer.Text = DTO.Trailer;

                cbNamSanXuat.Text = DTO.NamSanXuat.ToString();
                cbDinhDang.SelectedValue = DTO.MaDinhDangPhim;
                cbTrangThai.SelectedValue = DTO.MaTrangThaiPhim;

                ptbHinhAnhPhim.Image = new Bitmap("../../Images/" + DTO.Poster);

            }
            catch (Exception)
            {

            }
        }

        bool Check()
        {
            lbsao01.Visible = false;
            lbsao02.Visible = false;
            lbsao03.Visible = false;
            lbsao04.Visible = false;
            lbThongBao.Visible = false;

            if (txtTenPhim.Text == "")
            {
                lbsao01.Visible = true;
                txtTenPhim.Focus();
                return false;
            }
            else if (txtGioiThieuPhim.Text == "")
            {
                lbsao02.Visible = true;
                txtGioiThieuPhim.Focus();
                return false;
            }
            else if (txtHangSanXuat.Text == "")
            {
                lbsao03.Visible = true;
                txtHangSanXuat.Focus();
                return false;
            }
            else if (txtThoiLuong.Text == "")
            {
                lbsao04.Visible = true;
                txtThoiLuong.Focus();
                return false;
            }
            else
            {
                int num = 0;
                if (!int.TryParse(txtThoiLuong.Text, out num))
                {
                    lbThongBao.Visible = true;
                    txtThoiLuong.Focus();
                    return false;
                }
            }
            return true;
        }

    }
}
